<template>
  <div>
    <h2>房价添加</h2>
    <hr />
    <table>
      <tr>
        <td align="right">房间名称:</td>
        <td><input type="text" v-model="state.name" /></td>
      </tr>
      <tr>
        <td align="right">数量:</td>
        <td><input type="text" v-model="state.num" /></td>
      </tr>
      <tr>
        <td align="right">价格:</td>
        <td><input type="text" v-model="state.price" /></td>
      </tr>

      <tr>
        <td align="right"></td>
        <td><input type="button" value="添加" @click="login" /></td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import axios from 'axios';

const router = useRouter();
const route = useRoute();
let data: any = reactive({
  state: {
    name: '',
    num: 0,
    price: 0,
  },
});
let { state } = toRefs(data);

function login() {
  let obj = {
    name: state.value.name,
    num: state.value.num,
    price:state.value.price
  };
  axios({
    url: 'https://localhost:7105/api/FangPrice/Add',
    method: 'post',
    data: obj,
  })
    .then((res) => {
      console.log(res);
      if (res.data.data == -1) {
        alert('房间名称不存在');
        return;
      } else if (res.data.data > 0) {
        alert('添加成功');

        router.push('/Show');
      } else {
        alert('添加失败');
      }
    })
    .catch((err) => {
      console.log(err);
    });
}
</script>

<style scoped></style>
